{#
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
#}

{% extends base_template %}
{% from 'appbuilder/dag_docs.html' import dag_docs %}

{% block head_css %}
  {{ super() }}
  <link rel="stylesheet" type="text/css" href="{{ url_for_asset('codemirror.css') }}">
  <link rel="stylesheet" type="text/css" href="{{ url_for_asset('lint.css') }}">
  <link rel="stylesheet" type="text/css" href="{{ url_for_asset('switch.css') }}">
{% endblock %}

{% macro form_element(form_key, form_details) %}
<tr>
  <td class="col-lg-2">
    <label for="element_{{ form_key }}" control-label="">
      {%- if "title" in form_details.schema and form_details.schema.title -%}
        {{ form_details.schema.title }}
      {%- else -%}
        {{ form_key }}
      {%- endif -%}
      {%- if form_details.schema and form_details.schema.type and not "null" in form_details.schema.type and not "boolean" in form_details.schema.type -%}
        <strong style="color: red"> *</strong>
      {%- endif -%}
    : </label>
  </td>
  <td>
    {% if "custom_html_form" in form_details.schema %}
      {{ form_details.schema.custom_html_form | replace("{name}", "element_" + form_key) | replace("{value}", form_details.value) }}
    {% elif "type" in form_details.schema and form_details.schema.type == "boolean" %}
      <label for="element_{{ form_key }}" control-label="">
        <input class="switch-input" name="element_{{ form_key }}" id="element_{{ form_key }}" type="checkbox"
          {%- if form_details.value %} checked="checked"{%- endif -%}/>
        <span class="switch" aria-hidden="true"></span>
      </label>
    {% elif "format" in form_details.schema and "date-time" in form_details.schema.format %}
      <div class="input-group datetime datetimepicker">
        <span class="input-group-addon"><span class="material-icons cursor-hand">calendar_today</span></span>
        <input class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" type="text" valuetype="date"
          {%- if not "null" in form_details.schema.type %} required=""{%- endif %} value="{% if form_details.value %}{{ form_details.value }}{% endif %}" />
      </div>
    {% elif "format" in form_details.schema and "date" in form_details.schema.format %}
      <div class="input-group datetime datepicker">
        <span class="input-group-addon"><span class="material-icons cursor-hand">calendar_today</span></span>
        <input class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" type="text" valuetype="date"
          {%- if not "null" in form_details.schema.type %} required=""{%- endif %} value="{% if form_details.value %}{{ form_details.value }}{% endif %}" />
      </div>
    {% elif "format" in form_details.schema and "time" in form_details.schema.format %}
      <div class="input-group datetime timepicker">
        <span class="input-group-addon"><span class="material-icons cursor-hand">calendar_today</span></span>
        <input class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" type="text" valuetype="date"
          {%- if not "null" in form_details.schema.type %} required=""{%- endif %}
          value="{% if form_details.value %}{{ form_details.value[0:2] }}00-01-01 {{ form_details.value }}{% endif %}" />
      </div>
    {% elif "enum" in form_details.schema and form_details.schema.enum %}
      <select class="my_select2 form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" data-placeholder="Select Value"
        onchange="updateJSONconf();"
        {%- if "integer" in form_details.schema.type or "number" in form_details.schema.type %} valuetype="number"{% endif %}
        {%- if not "null" in form_details.schema.type %} required=""{% endif %}>
        {% for option in form_details.schema.enum -%}
          <option value="{{ option }}"
          {%- if option == form_details.value %} selected{% endif %}>
            {%- if form_details.schema.values_display and option|string in form_details.schema.values_display -%}
              {{ form_details.schema.values_display[option|string] }}
            {%- else -%}
              {{ option }}
            {%- endif -%}
          </option>
        {% endfor -%}
      </select>
    {% elif form_details.schema and "array" in form_details.schema.type %}
      {% if "items" in form_details.schema and form_details.schema["items"] and ("type" not in form_details.schema["items"] or "string" not in form_details.schema["items"]["type"]) %}
      <textarea class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" valuetype="advancedarray" rows="6"
        {%- if not "null" in form_details.schema.type %} required=""{% endif -%}>
        {%- if form_details.value is sequence %}
          {{- form_details.value | tojson() -}}
        {% endif -%}
      </textarea>
      {% elif "examples" in form_details.schema and form_details.schema.examples %}
      <select multiple name="element_{{ form_key }}" id="element_{{ form_key }}" class="select2-drop-mask" valuetype="multiselect"
        onchange="updateJSONconf();"{% if not "null" in form_details.schema.type %} required=""{% endif %}>
        {% for option in form_details.schema.examples -%}
          <option value="{{ option }}"
          {%- if option in form_details.value %} selected{% endif %}>
            {%- if form_details.schema.values_display and option|string in form_details.schema.values_display -%}
              {{ form_details.schema.values_display[option|string] }}
            {%- else -%}
              {{ option }}
            {%- endif -%}
          </option>
        {% endfor %}
      </select>
      {% else %}
      <textarea class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" valuetype="array" rows="6"
        {%- if not "null" in form_details.schema.type %} required=""{% endif -%}>
        {%- if form_details.value is sequence %}
          {%- for txt in form_details.value -%}
            {{ txt }}{{ "\n" }}
          {%- endfor -%}
        {% endif -%}
      </textarea>
      {% endif %}
    {% elif form_details.schema and "object" in form_details.schema.type %}
      <textarea class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" valuetype="object" rows="6"
        {%- if not "null" in form_details.schema.type %} required=""{% endif -%}>
        {{- form_details.value | tojson() -}}
      </textarea>
    {% elif form_details.schema and ("integer" in form_details.schema.type or "number" in form_details.schema.type) %}
      <input class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}"
             valuetype="number" {% if "integer" in form_details.schema.type %} type="number" {% else %} type="decimal" {% endif %}
        value="{% if form_details.value is not none %}{{ form_details.value }}{% endif %}"
        {%- if form_details.schema.minimum %} min="{{ form_details.schema.minimum }}"{% endif %}
        {%- if form_details.schema.maximum %} max="{{ form_details.schema.maximum }}"{% endif %}
        {%- if form_details.schema.type and not "null" in form_details.schema.type %} required=""{% endif %} />
    {% else %}
      <input class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}"
        type="{% if "examples" in form_details.schema and form_details.schema.examples %}search{% else %}text{% endif %}"
        value="{% if form_details.value %}{{ form_details.value }}{% endif %}"
        {%- if form_details.schema and form_details.schema.minLength %} minlength="{{ form_details.schema.minLength }}"{% endif %}
        {%- if form_details.schema and form_details.schema.maxLength %} maxlength="{{ form_details.schema.maxLength }}"{% endif %}
        {%- if "examples" in form_details.schema and form_details.schema.examples %} list="list_{{ form_key }}" placeholder="Start typing to see proposal values."{% endif %}
        {%- if form_details.schema and form_details.schema.type and not "null" in form_details.schema.type %} required=""{% endif %} />
      {% if "examples" in form_details.schema and form_details.schema.examples %}
        <datalist id="list_{{ form_key }}">
          {% for option in form_details.schema.examples -%}
          <option>{{ option }}</option>
          {% endfor -%}
        </datalist>
      {% endif %}
    {% endif %}
    {% if form_details.description -%}
      <span class="help-block">{{ form_details.description }}</span>
    {%- endif %}
  </td>
</tr>
{% endmacro %}

{% block content %}
  {{ super() }}
  <h2>
    Trigger DAG: <a href="{{ url_for('Airflow.'+ dag.get_default_view(), dag_id=dag.dag_id) }}" title="{{ dag.dag_id }}">{{ dag.dag_display_name }}</a>
    <small class="text-muted">{{ dag.description[0:150] + '…' if dag.description and dag.description|length > 150 else dag.description|default('', true) }}</small>
  </h2>
  {{ dag_docs(doc_md, False) }}
  <form method="POST" id="trigger_form">
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
    <input type="hidden" name="dag_id" value="{{ dag_id }}">
    <input type="hidden" name="origin" value="{{ origin }}">
    {% if recent_confs|length > 0 %}
    <div class="form-group row">
      <div class="col-md-2">
        <label for="recent_configs">Select Recent Configurations</label>
        <select class="form-control" name="recent_configs" id="recent_configs">
          <option value="{{ conf }}">Default parameters</option>
          {% for run_id, recent_conf in recent_confs.items() %}
          <option value="{{ recent_conf }}">{{ run_id }}: {{ recent_conf }}</option>
          {% endfor %}
        </select>
      </div>
    </div>
    {% endif %}
    {%- if form_fields %}
    <div class="panel panel-primary ">
      <div class="panel-heading">
        <h4 class="panel-title">DAG conf Parameters</h4>
      </div>
      <textarea style="display: none;" id="json_start" name="json_start">
        {{- conf -}}
      </textarea>
      <textarea style="display: none;" id="form_sections" name="form_sections">
        {{- form_fields.values() | map(attribute="schema.section", default="") | reject("equalto", "") | unique() | join(",") | lower() | replace(" ", "_") -}}
      </textarea>
      <div class="panel-body">

        <table class="table table-hover table-bordered">
          <tbody>
            {% for form_key, form_details in form_fields.items() %}
            {% if ("section" not in form_details.schema or not form_details.schema.section) and ("const" not in form_details.schema or not form_details.schema.const) %}
            {{ form_element(form_key, form_details) }}
            {% endif %}
            {% endfor %}
          </tbody>
        </table>

        {% for form_section, form_items in form_fields.values() | groupby(attribute="schema.section", default="") %}
        {% if form_section %}
        <div class="panel-group" id="accordion_section_{{ form_section | lower() | replace(' ', '_') }}">
          <div class="panel panel-default">
            <div class="panel-heading">
              <h4 class="panel-title">
                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion_section_{{ form_section | lower() | replace(' ', '_') }}" href="#accordion_section_{{ form_section | lower() | replace(' ', '_') }}_href"
                  data-original-title="" title="" id="{{ form_section | lower() | replace(' ', '_') }}_toggle">{{ form_section }}<span class="caret"></span></a>
              </h4>
            </div>
            <div id="accordion_section_{{ form_section | lower() | replace(' ', '_') }}_href" class="panel-collapse collapse">
              <div class="panel-body">
                <table class="table table-hover table-bordered">
                  <tbody>
                    {% for form_key, form_details in form_fields.items() %}
                    {% if form_details.schema.section == form_section and not form_details.schema.const %}
                    {{ form_element(form_key, form_details) }}
                    {% endif %}
                    {% endfor %}
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
        {% endif %}
        {% endfor %}

        <div class="panel-group" id="accordion_json_conf">
          <div class="panel panel-default">
            <div class="panel-heading">
              <h4 class="panel-title">
                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion_json_conf" href="#accordion_json_conf_href" data-original-title="" title="" id="generated_json_toggle">Generated Configuration JSON and Dagrun Options<span class="caret"></span></a>
              </h4>
            </div>
            <div id="accordion_json_conf_href" class="panel-collapse collapse">
              <div class="panel-body">
                <table class="table table-hover table-bordered">
                  <tbody>
                    <tr>
                      <td class="col-lg-2">
                        <label for="execution_date" control-label="">Logical date:</label>
                      </td>
                      <td>
                        {{ form.execution_date(class_="form-control", disabled=False) }}
                      </td>
                    </tr>
                    <tr>
                      <td class="col-lg-2">
                        <label for="run_id" control-label="">Run id:</label>
                      </td>
                      <td>
                        <input type="text" class="form-control" placeholder="Run id, optional - will be generated if not provided" name="run_id" id="run_id"{% if run_id %}value="{{ run_id }}"{% endif %}>
                      </td>
                    </tr>
                    <tr>
                      <td colspan="2">
                        <textarea name="conf" id="json">{{ conf }}</textarea>
                      </td>
                    </tr>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    {%- else %}
    <div class="form-group">
      <label for="execution_date">Logical date</label>
      <div class="input-group">
        {{ form.execution_date(class_="form-control", disabled=False) }}
      </div>
    </div>
    <div class="form-group row">
      <div class="col-md-2">
        <label for="run_id">Run id (Optional)</label>
        <input type="text" class="form-control" placeholder="Run ID" name="run_id"{% if run_id %}value="{{ run_id }}"{% endif %}>
      </div>
    </div>
    <label for="conf">Configuration JSON (Optional, must be a dict object)</label>
    <textarea class="form-control" name="conf" id="json">{{ conf }}</textarea>
    {%- endif %}
    {% if dag.get_is_paused() %}
    <div class="form-group">
      <label class="switch-label">
        <input class="switch-input" name="unpause" id="unpause" type="checkbox" checked>
        <span class="switch" aria-hidden="true"></span>
        Unpause DAG when triggered
      </label>
    </div>
    {% endif %}
    <button type="submit" class="btn btn-primary">Trigger</button>
    <a class="btn" href="{{ origin }}">Cancel</a>
  </form>
{% endblock %}

{% block tail_js %}
  <script src="{{ url_for_asset('codemirror.js') }}"></script>
  <script src="{{ url_for_asset('javascript.js') }}"></script>
  <script src="{{ url_for_asset('lint.js') }}"></script>
  <script src="{{ url_for_asset('javascript-lint.js') }}"></script>
  <script src="{{ url_for_asset('jshint.js') }}"></script>
  <script src="{{ url_for_asset('trigger.js') }}"></script>
  {{ super() }}
{% endblock %}
